package com.lyw.config.servlet;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.lyw.servlet.MyFilter;
import com.lyw.servlet.MyListener;
import com.lyw.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import java.util.Arrays;

@Deprecated
//@Configuration
public class ServletConfiguration {

//  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    //开启druid监控
    StatViewServlet statViewServlet = new StatViewServlet();
    ServletRegistrationBean<Servlet> registrationBean =
            new ServletRegistrationBean<>(statViewServlet, "/druid/*");

    //开启监控spring,登录验证
    registrationBean.addInitParameter("loginUsername", "admin");
    registrationBean.addInitParameter("loginPassword", "123");

//    MyServlet myServlet = new MyServlet();
//    servletServletRegistrationBean.setServlet(myServlet);
//    servletServletRegistrationBean.addUrlMappings("/my");


    return registrationBean;
  }

//  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    /*MyFilter myFilter = new MyFilter();
    return new FilterRegistrationBean(myFilter, servletRegistrationBean());*/
    //开启web监控
    WebStatFilter webStatFilter = new WebStatFilter();
    FilterRegistrationBean<Filter> registrationBean =
            new FilterRegistrationBean<>(webStatFilter);
    //设置拦截地址
    registrationBean.setUrlPatterns(Arrays.asList("/*"));
    //设置排除的拦截地址
    registrationBean.addInitParameter("exclusions",
            "*.jpg,*.js,*.css,*.png,*.gif,*.ico,*.otf,*.eot,*.svg,*.ttf,*.woff,*.woff2,/druid/**,/sql");
    return registrationBean;
  }

//  @Bean
//  public ServletListenerRegistrationBean servletListenerRegistrationBean() {
//    ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
//    bean.setListener(new MyListener());
//    return bean;
//  }

}
